home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / textwndw.swg / 0013_Graphics Win in Text Mode.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-02-03  |  13.4 KB  |  498 lines

  1. {
  2.  STG>Does anyone know off hand if I can be in text mode and window in a
  3.  STG>window and put the wondow only in graphics mode?
  4.  STG>I have a program that I need to have a graph in.  Does anyone have some
  5.  STG>code for using the PLOT procedure to plot variables.  The values for
  6.  STG>the Y axis are from 1 - 2000, and for the X axis from 1 - 24.
  7.  
  8.         Yes, it's possible... sort of.   If you have a VGA (or
  9. EGA) you can have 2 separate character sets on screen at once.
  10. Use one character set for text, and redefine the other for your
  11. graphics window.  The only problem is that your graphics window
  12. can only be composed of 256 characters total.  So, a 16 X 16
  13. character square would only give you a vertical resolution of 256
  14. pixels and a horizontal resolution of 128 pixels.  The following
  15. code is an example of how one would do this.
  16.  
  17.                                                 Dave
  18.  
  19. }
  20.  
  21. Program GraphicsInTextModeExample;
  22.  
  23. {================================================
  24.  
  25.          Graphics In Text Mode Example
  26.             Programmed by David Dahl
  27.                     12/24/93
  28.     This program and source are PUBLIC DOMAIN
  29.  
  30.  ------------------------------------------------
  31.  
  32.    This example uses a second font as a pseudo-
  33.    graphics window.  This program requires VGA.
  34.  
  35.  ================================================}
  36.  
  37. Uses  CRT;
  38.  
  39. Const { Dimentions of The Graphics Window in Characters }
  40.       ChrSizeX = 32;
  41.       ChrSizeY = 256 DIV ChrSizeX;
  42.       { Dimentions of The Graphics Window in Pixels }
  43.       MaxX     = ChrSizeX * 8;
  44.       MaxY     = ChrSizeY * 16;
  45.  
  46. {-[ Set Character Width to 8 Pixels ]-------------------------------------}
  47. Procedure SetCharWidthTo8; Assembler;
  48. Asm
  49.    { Change To 640 Horz Res }
  50.    MOV DX, $3CC
  51.    IN  AL, DX
  52.    AND AL, Not(4 OR 8)
  53.    MOV DX, $3C2
  54.    OUT DX, AL
  55.    { Turn Off Sequence Controller }
  56.    MOV DX, $3C4
  57.    MOV AL, 0
  58.    OUT DX, AL
  59.    MOV DX, $3C5
  60.    MOV AL, 0
  61.    OUT DX, AL
  62.    { Reset Sequence Controller }
  63.    MOV DX, $3C4
  64.    MOV AL, 0
  65.    OUT DX, AL
  66.    MOV DX, $3C5
  67.    MOV AL, 3
  68.    OUT DX, AL
  69.    { Switch To 8 Pixel Wide Fonts }
  70.    MOV DX, $3C4
  71.    MOV AL, 1
  72.    OUT DX, AL
  73.    MOV DX, $3C5
  74.    IN  AL, DX
  75.    OR  AL, 1
  76.    OUT DX, AL
  77.    { Turn Off Sequence Controller }
  78.    MOV DX, $3C4
  79.    MOV AL, 0
  80.    OUT DX, AL
  81.    MOV DX, $3C5
  82.    MOV AL, 0
  83.    OUT DX, AL
  84.    { Reset Sequence Controller }
  85.    MOV DX, $3C4
  86.    MOV AL, 0
  87.    OUT DX, AL
  88.    MOV DX, $3C5
  89.    MOV AL, 3
  90.    OUT DX, AL
  91.    { Center Screen }
  92.    MOV DX, $3DA
  93.    IN  AL, DX
  94.    MOV DX, $3C0
  95.    MOV AL, $13 OR 32
  96.    OUT DX, AL
  97.    MOV AL, 0
  98.    OUT DX, AL
  99. End;
  100. {-[ Turn On Dual Fonts ]--------------------------------------------------}
  101. Procedure SetDualFonts; Assembler;
  102. ASM
  103.    { Set Fonts 0 & 1 }
  104.    MOV BL, 4
  105.    MOV AX, $1103
  106.    INT $10
  107. END;
  108. {-[ Turn On Access To Font Memory ]---------------------------------------}
  109. Procedure SetAccessToFontMemory; Assembler;
  110. ASM
  111.    { Turn Off Sequence Controller }
  112.    MOV DX, $3C4
  113.    MOV AL, 0
  114.    OUT DX, AL
  115.    MOV DX, $3C5
  116.    MOV AL, 1
  117.    OUT DX, AL
  118.    { Reset Sequence Controller }
  119.    MOV DX, $3C4
  120.    MOV AL, 0
  121.    OUT DX, AL
  122.    MOV DX, $3C5
  123.    MOV AL, 3
  124.    OUT DX, AL
  125.    { Change From Odd/Even Addressing to Linear }
  126.    MOV DX, $3C4
  127.    MOV AL, 4
  128.    OUT DX, AL
  129.    MOV DX, $3C5
  130.    MOV AL, 7
  131.    OUT DX, AL
  132.    { Switch Write Access To Plane 2 }
  133.    MOV DX, $3C4
  134.    MOV AL, 2
  135.    OUT DX, AL
  136.    MOV DX, $3C5
  137.    MOV AL, 4
  138.    OUT DX, AL
  139.    { Set Read Map Reg To Plane 2 }
  140.    MOV DX, $3CE
  141.    MOV AL, 4
  142.    OUT DX, AL
  143.    MOV DX, $3CF
  144.    MOV AL, 2
  145.    OUT DX, AL
  146.    { Set Graphics Mode Reg }
  147.    MOV DX, $3CE
  148.    MOV AL, 5
  149.    OUT DX, AL
  150.    MOV DX, $3CF
  151.    MOV AL, 0
  152.    OUT DX, AL
  153.    { Set Misc. Reg }
  154.    MOV DX, $3CE
  155.    MOV AL, 6
  156.    OUT DX, AL
  157.    MOV DX, $3CF
  158.    MOV AL, 12
  159.    OUT DX, AL
  160. End;
  161. {-[ Turn On Access to Text Memory ]---------------------------------------}
  162. Procedure SetAccessToTextMemory; Assembler;
  163. ASM
  164.    { Turn Off Sequence Controller }
  165.    MOV DX, $3C4
  166.    MOV AL, 0
  167.    OUT DX, AL
  168.    MOV DX, $3C5
  169.    MOV AL, 1
  170.    OUT DX, AL
  171.    { Reset Sequence Controller }
  172.    MOV DX, $3C4
  173.    MOV AL, 0
  174.    OUT DX, AL
  175.    MOV DX, $3C5
  176.    MOV AL, 3
  177.    OUT DX, AL
  178.    { Change To Odd/Even Addressing }
  179.    MOV DX, $3C4
  180.    MOV AL, 4
  181.    OUT DX, AL
  182.    MOV DX, $3C5
  183.    MOV AL, 3
  184.    OUT DX, AL
  185.    { Switch Write Access }
  186.    MOV DX, $3C4
  187.    MOV AL, 2
  188.    OUT DX, AL
  189.    MOV DX, $3C5
  190.    MOV AL, 3  {?}
  191.    OUT DX, AL
  192.    { Set Read Map Reg }
  193.    MOV DX, $3CE
  194.    MOV AL, 4
  195.    OUT DX, AL
  196.    MOV DX, $3CF
  197.    MOV AL, 0
  198.    OUT DX, AL
  199.    { Set Graphics Mode Reg }
  200.    MOV DX, $3CE
  201.    MOV AL, 5
  202.    OUT DX, AL
  203.    MOV DX, $3CF
  204.    MOV AL, $10
  205.    OUT DX, AL
  206.    { Set Misc. Reg }
  207.    MOV DX, $3CE
  208.    MOV AL, 6
  209.    OUT DX, AL
  210.    MOV DX, $3CF
  211.    MOV AL, 14
  212.    OUT DX, AL
  213. End;
  214. {-[ Clear The Pseudo-Graphics Window by Clearing Font Definition ]--------}
  215. Procedure ClearGraphicsWindow;
  216. Begin
  217.      SetAccessToFontMemory;
  218.      FillChar (MEM[$B800:$4000], 32 * 256, 0);
  219.      SetAccessToTextMemory;
  220. End;
  221. {-[ Turn The Cursor Off ]-------------------------------------------------}
  222. Procedure TurnCursorOff; Assembler;
  223. ASM
  224.    MOV DX, $3D4
  225.    MOV AL, $0A
  226.    OUT DX, AL
  227.    MOV DX, $3D5
  228.    IN  AL, DX
  229.    OR  AL, 32
  230.    OUT DX, AL
  231. End;
  232. {-[ Turn The Cursor On ]--------------------------------------------------}
  233. Procedure TurnCursorOn; Assembler;
  234. ASM
  235.    MOV DX, $3D4
  236.    MOV AL, $0A
  237.    OUT DX, AL
  238.    MOV DX, $3D5
  239.    IN  AL, DX
  240.    AND AL, Not(32)
  241.    OUT DX, AL
  242. End;
  243. {-[ Set Up The Pseudo-Graphics Window ]-----------------------------------}
  244. Procedure SetGraphicsWindow (XCoord, YCoord    : Byte;
  245.                              Color, BackGround : Byte);
  246. Var CounterX,
  247.     CounterY  : Byte;
  248. Begin
  249.      For CounterY := 0 to (ChrSizeY-1) do
  250.          For CounterX := 0 to (ChrSizeX-1) do
  251.              MEMW[$B800:CounterX*2 + XCoord*2 + (YCoord * 80 * 2) +
  252.                  (CounterY * 80 * 2)] :=
  253.                    (CounterX + CounterY * ChrSizeX) OR
  254.                    (((Color OR 8) OR ((BackGround AND 15) SHL 4)) SHL 8);
  255. End;
  256. {-[ Plot a Pixel in The Pseudo-Graphics Window ]--------------------------}
  257. Procedure PutPixel (Xin, Yin : Word);
  258. Var RealY,
  259.     RealX      : Word;
  260. Begin
  261.      If (Xin < MaxX) AND
  262.         (Yin < MaxY)
  263.      Then
  264.      Begin
  265.           RealX := (Xin DIV 8) * 32;
  266.           RealY := (Yin MOD 16) + ((Yin DIV 16) * (32 * ChrSizeX));
  267.           SetAccessToFontMemory;
  268.           MEM[$B800:$4000 + RealX + RealY] :=
  269.               MEM[$B800:$4000 + RealX + RealY] OR (128 SHR (Xin MOD 8));
  270.           SetAccessToTextMemory;
  271.      End;
  272. End;
  273. {-[ Draw A Line ]---------------------------------------------------------}
  274. { OCTANT DDA Subroutine converted from the BASIC listing on pages 26 - 27 }
  275. { from the book _Microcomputer_Displays,_Graphics,_ And_Animation_ by     }
  276. { Bruce A. Artwick                                                        }
  277. Procedure Line (XStart, YStart, XEnd, YEnd : Word);
  278. Var StartX,
  279.     StartY,
  280.     EndX,
  281.     EndY    : Word;
  282.     DX,
  283.     DY      : Integer;
  284.     CNTDWN  : Integer;
  285.     Errr    : Integer;
  286.     Temp    : Integer;
  287.     NotDone : Boolean;
  288. Begin
  289.      NotDone := True;
  290.      StartX := XStart;
  291.      StartY := YStart;
  292.      EndX   := XEnd;
  293.      EndY   := YEnd;
  294.      If EndX < StartX Then
  295.      Begin
  296.           { Mirror Quadrants 2,3 to 1,4 }
  297.           Temp   := StartX;
  298.           StartX := EndX;
  299.           EndX   := Temp;
  300.           Temp   := StartY;
  301.           StartY := EndY;
  302.           EndY   := Temp;
  303.      End;
  304.      DX := EndX - StartX;
  305.      DY := EndY - StartY;
  306.      If DY < 0 Then
  307.      Begin
  308.           If -DY > DX Then
  309.           Begin
  310.                { Octant 7 Line Generation }
  311.                CntDwn := -DY + 1;
  312.                ERRR   := -(-DY shr 1);   {Fast Divide By 2}
  313.                While NotDone do
  314.                Begin
  315.                     PutPixel (StartX, StartY);
  316.                     Dec (CntDwn);
  317.                     If CntDwn <= 0
  318.                     Then NotDone := False
  319.                     Else
  320.                     Begin
  321.                          Dec(StartY);
  322.                          Inc(Errr, DX);
  323.                          If Errr >= 0 Then
  324.                          Begin
  325.                               Inc(StartX);
  326.                               Inc(Errr, DY);
  327.                          End;
  328.                     End;
  329.                End;
  330.           End
  331.           Else
  332.           Begin
  333.                { Octant 8 Line Generation }
  334.                CntDwn := DX + 1;
  335.                ERRR   := -(DX shr 1);   {Fast Divide By 2}
  336.                While NotDone do
  337.                Begin
  338.                     PutPixel (StartX, StartY);
  339.                     Dec (CntDwn);
  340.                     If CntDwn <= 0
  341.                     Then NotDone := False
  342.                     Else
  343.                     Begin
  344.                          Inc(StartX);
  345.                          Dec(Errr, DY);
  346.                          If Errr >= 0 Then
  347.                          Begin
  348.                               Dec(StartY);
  349.                               Dec(Errr, DX);
  350.                          End;
  351.                     End;
  352.                End;
  353.           End;
  354.      End
  355.      Else If DY > DX Then
  356.           Begin
  357.                { Octant 2 Line Generation }
  358.                CntDwn := DY + 1;
  359.                ERRR   := -(DY shr 1);   {Fast Divide By 2}
  360.                While NotDone do
  361.                Begin
  362.                     PutPixel (StartX, StartY);
  363.                     Dec (CntDwn);
  364.                     If CntDwn <= 0
  365.                     Then NotDone := False
  366.                     Else
  367.                     Begin
  368.                          Inc(StartY);
  369.                          Inc(Errr, DX);
  370.                          If Errr >= 0 Then
  371.                          Begin
  372.                               Inc(StartX);
  373.                               Dec(Errr, DY);
  374.                          End;
  375.                     End;
  376.                End;
  377.           End
  378.           Else
  379.           { Octant 1 Line Generation }
  380.           Begin
  381.                CntDwn := DX + 1;
  382.                ERRR   := -(DX shr 1);   {Fast Divide By 2}
  383.                While NotDone do
  384.                Begin
  385.                     PutPixel (StartX, StartY);
  386.                     Dec (CntDwn);
  387.                     If CntDwn <= 0
  388.                     Then NotDone := False
  389.                     Else
  390.                     Begin
  391.                          Inc(StartX);
  392.                          Inc(Errr, DY);
  393.                          If Errr >= 0 Then
  394.                          Begin
  395.                               Inc(StartY);
  396.                               Dec(Errr, DX);
  397.                          End;
  398.                     End;
  399.                End;
  400.           End;
  401. End;
  402. {-[ Draw A Circle ]-----------------------------------------------------}
  403. { Algorithm based on the Pseudocode from page 83 of the book _Advanced  }
  404. { Graphics_In_C_ by Nelson Johnson                                      }
  405. Procedure Circle (XCoord, YCoord, Radius : Integer);
  406. Var   d     : Integer;
  407.       X, Y  : Integer;
  408.     Procedure Symmetry (xc, yc, x, y : integer);
  409.     Begin
  410.          PutPixel ( X+xc,  Y+yc);
  411.          PutPixel ( X+xc, -Y+yc);
  412.          PutPixel (-X+xc, -Y+yc);
  413.          PutPixel (-X+xc,  Y+yc);
  414.          PutPixel ( Y+xc,  X+yc);
  415.          PutPixel ( Y+xc, -X+yc);
  416.          PutPixel (-Y+xc, -X+yc);
  417.          PutPixel (-Y+xc,  X+yc);
  418.     End;
  419. Begin
  420.      x := 0;
  421.      y := abs(Radius);
  422.      d := 3 - 2 * y;
  423.      While (x < y) do
  424.      Begin
  425.           Symmetry (XCoord, YCoord, x, y);
  426.           if (d < 0) Then
  427.              inc(d, (4 * x) + 6)
  428.           else
  429.           Begin
  430.                inc (d, 4 * (x - y) + 10);
  431.                dec (y);
  432.           End;
  433.           inc(x);
  434.      End;
  435.      If x = y then
  436.         Symmetry (XCoord, YCoord, x, y);
  437. End;
  438. {-[ Draw A Rectangle ]----------------------------------------------------}
  439. Procedure Rectangle (X1, Y1, X2, Y2 : Word);
  440. Begin
  441.      { Draw Top Of Box }
  442.      Line (X1, Y1, X2, Y1);
  443.      { Draw Right Side Of Box }
  444.      Line (X2, Y1, X2, Y2);
  445.      { Draw Left Side Of Box }
  446.      Line (X1, Y1, X1, Y2);
  447.      { Draw Botton Of Box }
  448.      Line (X1, Y2, X2, Y2);
  449. End;
  450. {=[ Main Program ]========================================================}
  451.  
  452. Var C : Word;
  453.     Key : Char;
  454. Begin
  455.  
  456.      TextMode (C80);
  457.      TurnCursorOff;
  458.      SetCharWidthTo8;
  459.      SetDualFonts;
  460.      ClearGraphicsWindow;
  461.      TextColor(LightGray);
  462.      ClrScr;
  463.  
  464.      SetGraphicsWindow (40, 0, White, Blue);   {X, Y, Color, BGColor}
  465.  
  466.      Writeln ('Graphics In Text Mode Example');
  467.      Writeln ('Programmed by David Dahl');
  468.      Writeln ('This is PUBLIC DOMAIN');
  469.      Writeln;
  470.      Writeln ('The graphics window to the right is');
  471.      Writeln ('made up of custom characters of the');
  472.      Writeln ('second font.');
  473.      Writeln;
  474.      Writeln ('There are four graphics primitives');
  475.      Writeln ('available in this example program.');
  476.      Writeln ('Circle, Line, PutPixel, and ');
  477.      Writeln ('Rectangle are avaiable for your own');
  478.      Writeln ('use.');
  479.      Writeln;
  480.  
  481.      Randomize;
  482.      For C := 1 to 10 do
  483.      Begin
  484.           Line (Random(MaxX), Random(MaxY),
  485.                 Random(MaxX), Random(MaxY));
  486.  
  487.           Circle (Random(MaxX), Random(MaxY), Random(30));
  488.  
  489.           Rectangle (Random(MaxX), Random(MaxY),
  490.                      Random(MaxX), Random(MaxY));
  491.      End;
  492.  
  493.      Writeln ('Press [RETURN] to exit.');
  494.      Readln;
  495.      TurnCursorOn;
  496.      TextMode (C80);
  497. End.
  498.